package pers.whj.util.list;

/**
 * the element in PriorityQueue must implement the Priority interface or Comparable interface
 * can fix the length of queue or don't fix length
 *
 * @param <T> the type of element
 */
public class PriorityQueue<T> extends MaxHeap<T> implements Queue<T> {
    public PriorityQueue() {
        super();
    }

    public PriorityQueue(T[] data) {
        super(data);
    }

    /**
     * @param maxLength the length limit of this queue
     */
    public PriorityQueue(int maxLength) {
        super(maxLength);
        setCanResize(false);
    }

    @Override
    public int maxLength() {
        return canResize() ? Integer.MAX_VALUE : getMaxLength();
    }

    @Override
    public boolean offer(T data) {
        checkWhileInsert();
        return super.insert(data);
    }

    @Override
    public T poll() {
        return super.delete();
    }

    @Override
    public T peek() {
        return super.getHead();
    }

    @Override
    public boolean isFull() {
        return mLength == maxLength();
    }

    @Override
    protected void checkWhileInsert() {
        if (!canResize() && mLength == getMaxLength()) {
            throw new IllegalStateException("queue is full, cannot offer any data");
        }
    }
}